﻿using ProjScan.Submodules.Shader_Analysis;
using ProjScan.Tools;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace ProjScan.RecordManager.AssetData
{
    internal class ShaderAssetData : ImportAssetData<Shader, ShaderImporter>
    {
        protected ShaderPassKeyManager shaderPassKeyManager;

        public int textureCount;

        public ulong variantNumber;

        public int globalFeatureSetCount;

        public int localFeatureSetCount;

        public int globalMultiCompileSetCount;

        public int localMultiCompileSetCount;

        public ShaderAssetData(int id, string path, Shader shader, ShaderImporter shaderImporter)
            : base(id, path, shader, shaderImporter)
        {
            textureCount = 0;
            globalFeatureSetCount = 0;
            localFeatureSetCount = 0;
            globalMultiCompileSetCount = 0;
            localMultiCompileSetCount = 0;
            if (shaderImporter == null)
            {
                noImporter = true;
            }

            ShaderPassKeyManager shaderPassKeyManager = new ShaderPassKeyManager
            {
                shaderName = shader.name,
                path = path
            };
            ShaderManager.I.AdjustManager(shaderPassKeyManager);
            variantNumber = ShaderManager.GetShaderVariantCount(shader);
            globalFeatureSetCount = shaderPassKeyManager.globalFeatureSet.Count;
            localFeatureSetCount = shaderPassKeyManager.localFeatureSet.Count;
            globalMultiCompileSetCount = shaderPassKeyManager.globalMultiCompileSet.Count;
            localMultiCompileSetCount = shaderPassKeyManager.localMultiCompileSet.Count;
            textureCount = GetTextureCount(shader);
            assetPathId = id;
            this.shaderPassKeyManager = shaderPassKeyManager;
        }

        public static int GetTextureCount(Shader shader)
        {
            int num = 0;
            int propertyCount = ShaderUtil.GetPropertyCount(shader);
            for (int i = 0; i < propertyCount; i++)
            {
                if (ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv)
                {
                    num++;
                }
            }

            return num;
        }

        public override string GetCSVHeader()
        {
            return "Name,DirID,TexCount,VN,SFG,SFL,MCG,MCL";
        }

        public override string GetInfo()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(ToolsHelper.QuoteString(fileName));
            stringBuilder.Append(',');
            stringBuilder.Append(assetPathId);
            stringBuilder.Append(',');
            stringBuilder.Append(textureCount);
            stringBuilder.Append(',');
            stringBuilder.Append(variantNumber);
            stringBuilder.Append(',');
            stringBuilder.Append(globalFeatureSetCount);
            stringBuilder.Append(',');
            stringBuilder.Append(localFeatureSetCount);
            stringBuilder.Append(',');
            stringBuilder.Append(globalMultiCompileSetCount);
            stringBuilder.Append(',');
            stringBuilder.Append(localMultiCompileSetCount);
            return stringBuilder.ToString();
        }
    }
}